{% extends "base/front.html" %}

{% block title %}Interest Blog User Profile | SaintIC{% endblock %}

{% block content %}

<article class="post">

  <header class="post-header">
    <p>
        <strong><h2>User Profile Change</h2></strong>
    </p>
  </header>

  <div class="post-content">

    {% if request.args.get("change") == "avatar" %}
        <p>
        {%- if user.get('avatar') == url_for('static', filename='img/avatar/default.jpg') %}
            <scan>{{ user.get('cname') or '嘿' }}，你当前使用系统默认头像。</scan>
        {% else %}
            <img src="{{ user.get('avatar') }}" onload="if(this.width >= 320){this.width = 320}">
        {% endif -%}
        </p>
        <form action="{{ url_for('upload.UploadProfileAvatar') }}" method="post" enctype="multipart/form-data">
            <p>选择头像图片(仅支持jpg/jpeg/png/gif)：</p>
            <p><input type="file" name="file" /><p />
            <input type="submit" name="submit" value="更新头像" class="button green" />
        </form>
    {% elif request.args.get("change") == "profile" %}
        <form id="UpdateUserFormId" method="" action="">
            <p><label for="male">我的别名：</label><input type="text" style="width:300px;height:20px" name="cname" value="{{ user.cname or '' }}" placeholder="请在这里写上您的昵称吧" /></p>
            <p><label for="male">我的网址：</label><input type="url" style="width:300px;height:20px" name="url" value="{{ user.url or '' }}" placeholder="请在这里写上您的个人网址吧" /></p>
            <p><label for="male">我的格言：</label><input type="text" style="width:300px;height:20px" name="motto" value="{{ user.motto or '' }}" placeholder="请在这里写上您的座右铭吧" /></p>
            <p><label for="male">我的代码：</label><input type="url" style="width:300px;height:20px" name="github" value="{{ user.github or '' }}" placeholder="请在这里写上您的GitHub地址吧" /></p>
            <p><label for="male">我的微博：</label><input type="url" style="width:300px;height:20px" name="weibo" value="{{ user.weibo or '' }}" placeholder="请在这里写上您的Weibo地址吧" /></p>
            <p><label for="male">我的邮箱：</label><input type="email" style="width:300px;height:20px" name="email" value="{{ user.email or '' }}" placeholder="请在这里写上您的公开邮箱吧" /></p>
            <p><label for="male">选择性别：</label>
            {%- if user.gender == '女' %}
                <input type="radio" name="gender" value="男">男
                <input type="radio" name="gender" value="女" checked="checked">女
            {% else %}
                <input type="radio" name="gender" value="男" checked="checked">男
                <input type="radio" name="gender" value="女">女
            {% endif -%}
            </p>
            <input type="hidden" name="username" value="{{ g.username }}">
            <input type="reset" value="内容重置" class="button green" style="float: left;" />
            <input onclick=putForm() type="button" value="更新资料" class="button green" />
        </form>
    {% elif request.args.get("change") == "password" %}
        <form method="post" action="{{ url_for('front.UpdatePasswd') }}">
            <p><label for="male">请输入旧密码：</label><input type="password" style="width:300px;height:20px" name="old_pass" value="" placeholder="请输入旧密码" /></p>
            <p><label for="male">请输入新密码：</label><input type="password" style="width:300px;height:20px" name="new_pass" value="" placeholder="请输入新密码" /></p>
            <p><label for="male">请确认新密码：</label><input type="password" style="width:300px;height:20px" name="new_repass" value="" placeholder="请确认新密码" /></p>
            <input type="hidden" name="username" value="{{ g.username }}">
            <input type="reset" value="内容重置" class="button green" style="float: left;" />
            <input type="submit" value="更新密码" class="button green" />
        </form>
    {% endif %}

</article>

{% endblock %}

{% block script %}
{% if request.args.get("change") == "profile" %}
<script type="text/javascript">
    // 更新用户资料
    function putForm() {
        $.ajax({
            url: "{{ g.apiurl + '/user/' }}",
            method: 'PUT',
            datatype: 'json',
            data: $('#UpdateUserFormId').serialize(),
            success: function(res) {
                console.log(res);
                if (res.success == true){
                    alert("更新用户资料成功！");
                    window.location="{{ url_for('front.home') }}";
                } else {
                    alert("更新用户资料失败，请重试提交或报告错误！\n错误原因是：" + res.msg);
                    //location.reload(true);
                }
            },
        });
    };
</script>
{% endif %}
{% endblock %}
